/*
 * @Description:
 * @User: Vijay <1937832819@qq.com>
 * @Date: 2020-10-20 22:34:11
 */

package middlewares

import (
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/plugins/cors"
)

//Response map[string]interface{}{"code": 0, "msg": "no user exists!", "data": nil}
type Response struct {
	Code       int         `json:"code"`
	Msg        string      `json:"msg"`
	Data       interface{} `json:"data"`
	CreateTime string      `json:"create_time"`
}

// OutResponse OutResponse
func OutResponse(code int, msg string, data interface{}) Response {
	Resp := Response{
		Code:       code,
		Msg:        msg,
		Data:       data,
		CreateTime: beego.Date(time.Now(), "Y-m-d H:i:s"),
	}
	return Resp
}

/**
添加解决跨域请求问题，文件下载文件夹，支持表单伪造PUT,DELETE,PATCH,OPTIONS请求
*/
func CorsDomain() {
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowAllOrigins:  true,
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		AllowCredentials: true,
	}))
}
